package com.jcgyl.export.service.Impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jcgyl.export.dao.DepartmentDao;
import com.jcgyl.export.entity.Department;
import com.jcgyl.export.entity.Tbsysuser;
import com.jcgyl.export.mapper.DepartmentMapper;
import com.jcgyl.export.service.DepartmentService;
import com.jcgyl.export.util.entity.Result;

@Service
public class DepartmentServiceImpl implements DepartmentService {
	@Autowired
	private DepartmentDao departmentDao;
	@Autowired
	private DepartmentMapper departmentMapper;

	@Override
	public Map<String, Object> findAllDepartment(String departmentName, String parentName, int level, int pageSize,
			int pageIndex) {
		// 总行数
		int rowCount = departmentDao.findRowCount(departmentName, parentName, level);
		int startIndex = (pageIndex - 1) * pageSize;
		List<Department> list = departmentDao.findAllDepartment(departmentName, parentName, level, pageSize,
				startIndex);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("result", list);
		map.put("totalRows", rowCount);
		return map;
	}


	/** 添加部门 */
	@Override
	public Result addDepartment(Department department) {
		Tbsysuser user=(Tbsysuser)SecurityUtils.getSubject().getSession().getAttribute("user");
		String departmentName = department.getDepartmentName();
		String parentName = department.getParentName();
		System.out.println(" 添加部门    departmentName:" + departmentName + "parentName" + parentName);
		try {
			if (department.getDepartmentName() == "" || department.getParentName() == "") {
				return Result.build(1, "部门名称和上级部门不能为空");
			}
			if ( departmentDao.findByDepartmentName(departmentName).getDepartmentName()!=null) {
				return Result.build(1, department.getDepartmentName()+ "此部门已存在");
			}
			if (department.getLevel() == null) {
				Result.build(1, department.getLevel() + "请选择部门等级");
			}
			if (departmentDao.findByParentName(parentName).getParentName()!=null) {
				return Result.build(1, department.getParentName() + "此上级部门不存在");
			}	
			int id=departmentDao.findByParentName(parentName).getpId();
			department.setpId(id);
			department.setModifiedUser(user.getUname());
			department.setCreatedTime(new Date());
			departmentMapper.insert(department);
			return Result.ok("添加成功");
		} catch (Exception e) {
			e.printStackTrace();
			return Result.build(1, e.getCause().getMessage());
		}
	}


	@Override
	public List<Department> findDepartmentName() {
		return departmentDao.findAllDepartmentName();
	}

	/** 修改部门 */
	@Override
	public Result updateDepartment(Department department) {
		Tbsysuser user=(Tbsysuser)SecurityUtils.getSubject().getSession().getAttribute("user");
		String departmentName = department.getDepartmentName();
		String parentName = department.getParentName();
		System.out.println(" 修改部门    departmentName:" + departmentName + "parentName" + parentName);
		try {
			if (department.getDepartmentName() == "" || department.getParentName() == "") {
				return Result.build(1, "部门名称和上级部门不能为空");
			}
			if (departmentDao.findByParentName(parentName).getParentName()==null) {
				return Result.build(1, department.getParentName() + "此上级部门不存在");
			}	
			Department department_update=departmentDao.findById(department.getId());
			int pId=departmentDao.findByParentName(parentName).getpId();
			department_update.setpId(pId);
			department_update.setDepartmentName(department.getDepartmentName());
			department_update.setLevel(department.getLevel());
			department_update.setIsEnd(department.getIsEnd());
			department_update.setNote(department.getNote());
			department_update.setModifiedUser(user.getUname());;
			department_update.setModifiedTime(new Date());
			departmentMapper.updateByPrimaryKey(department_update);
			return Result.ok("修改成功!");
		} catch (Exception e) {
			e.printStackTrace();
			return Result.build(1, e.getCause().getMessage());
		}
	}

	/** 删除部门 */
	@Override
	public Result deleteDepartment(int departmentId) {
		try {
			departmentMapper.deleteByPrimaryKey(departmentId);
			return Result.ok("删除成功!");
		} catch (Exception e) {
			e.printStackTrace();
			return Result.build(1, e.getCause().getMessage());
		}
	}


	@Override
	public int findRowCount(String departmentName, String parentName, int level) {
		int  rowCount=departmentDao.findRowCount(departmentName, parentName, level);
		return rowCount;
	}


	@Override
	public List<Department> findZtreeObject() {
		return departmentDao.findZtreeObject();
	}



}
